//
//  MJAnswerViewController.m
//  MJ
//
//  Created by lina on 2017/12/11.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJAnswerViewController.h"
#import "MJHomeModel.h"
#import "MJQuestionsModel.h"
#import "MJExamViewController.h"
#import <LoveAdSDK/LoveAdSDK.h>
#import "MJQuizzesViewModel.h"
#import "MJHomeViewController.h"

@import LoveAdSDK;

@interface MJAnswerViewController ()

/** _reTestButton */
@property (nonatomic, strong) UIButton *reTestButton;

/** _nextTestButton */
@property (nonatomic, strong) UIButton *nextTestButton;

@end

@implementation MJAnswerViewController{
    CGFloat adHeight;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    adHeight = kScreenWidth/1080*300;
    [self setupUI];
    [self setupAD];
    [self setupActions];
}

- (void)setupAD{
    
    AdView *adView = [[AdView alloc] initWithFrame:CGRectMake(0, kScreenHeight-adHeight, self.view.frame.size.width, adHeight) type:AdTypeWindow appId:@"23cec886-270f-4096-81d7-4f9734ff15fc"];

    [self.view addSubview:adView];
}

- (void)setupUI{
    
    self.navgationBar.titleLabel.text = self.quizzes.title;
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text = @"测试结果";
    tipLabel.textColor = HEXColor(@"#bfbfbf");
    tipLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(15);
        make.top.mas_equalTo(self.navgationBar.mas_bottom).offset(20);
    }];
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.numberOfLines = 0;
    titleLabel.text = self.anwser.head;
    titleLabel.textColor = HEXColor(@"#666666");
    titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(15);
        make.top.mas_equalTo(tipLabel.mas_bottom).offset(15);
        make.right.mas_equalTo(self.view).offset(-15);
    }];
    
    UITextView *describeLabel = [[UITextView alloc] init];
    describeLabel.backgroundColor = [UIColor clearColor];
//    describeLabel.numberOfLines = 0;
    describeLabel.text = self.anwser.desc;
    describeLabel.textColor = HEXColor(@"#8c8c8c");
    describeLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:describeLabel];
    [describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(15);
        make.right.mas_equalTo(self.view).offset(-15);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(15);
        make.bottom.mas_equalTo(self.view).offset(-adHeight-40-20);
    }];
    
    _reTestButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_reTestButton setTitle:@"重新测试" forState:UIControlStateNormal];
    [_reTestButton setTitleColor:HEXColor(@"ff8280") forState:UIControlStateNormal];
    _reTestButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:_reTestButton];
    ViewBorderRadius(_reTestButton, 20, 1, HEXColor(@"#ff8280"));
    [_reTestButton addTarget:self action:@selector(retest) forControlEvents:UIControlEventTouchUpInside];
    [_reTestButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view).offset(-kScreenWidth/4);
        make.bottom.mas_equalTo(self.view).offset(-adHeight-20);
        make.size.mas_equalTo(CGSizeMake(150, 40));
    }];
    
    
    _nextTestButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_nextTestButton setTitle:@"下一测试" forState:UIControlStateNormal];
    [_nextTestButton setTitleColor:HEXColor(@"#ffffff") forState:UIControlStateNormal];
    _nextTestButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_nextTestButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextTestButton];
    _nextTestButton.backgroundColor = HEXColor(@"#ff8280");
    ViewBorderRadius(_nextTestButton, 20, 0, HEXColor(@"#ff8280"));
    [_nextTestButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view).offset(kScreenWidth/4);
        make.bottom.mas_equalTo(self.reTestButton).offset(0);
        make.size.mas_equalTo(CGSizeMake(150, 40));
    }];

    if ([[self.quizzesArr lastObject].ID isEqualToString:self.quizzes.ID]) {
        _nextTestButton.alpha = .5;
        _nextTestButton.enabled = NO;
    }
}

- (void)back{
    
    if (self.quizzesArr.count == 0) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
        for (UIViewController *controller in self.navigationController.viewControllers) {
            
            if ([controller isKindOfClass:[MJHomeViewController class]]){
                
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
}

- (void)retest{
    
    MJExamViewController *examVc = [[MJExamViewController alloc] init];
    examVc.quizzes = self.quizzes;
    examVc.quizzesArr = self.quizzesArr;
    [self.navigationController pushViewController:examVc animated:YES];
}

- (void)next{
    
    MJExamViewController *examVc = [[MJExamViewController alloc] init];
    NSInteger i = [self.quizzesArr indexOfObject:self.quizzes];
    NSLog(@"%ld",i);
    examVc.quizzes = self.quizzesArr[[self.quizzesArr indexOfObject:self.quizzes]+1];
    examVc.quizzesArr = self.quizzesArr;
    [self.navigationController pushViewController:examVc animated:YES];
}

- (void)setupActions{
    
    if (self.quizzesArr.count == 0) {
        self.reTestButton.hidden = self.nextTestButton.hidden = YES;
    }else{
        
    }
}
@end
